﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using Sustainalytics.Entities.ProductInvolvement;

using System.Collections.Generic;
using System.Linq;
using System.Net.Http;

namespace Sustainalytics.ProductInvolvement.Service.Controllers.Test
{
    using Sustainalytics.UtilsTests;

    [TestClass]
    public class PIIndicatorCategoriesControllerTests
    {
        private List<PICategory> piCategories;

        [TestInitialize]
        public void Initialize()
        {
            piCategories = new List<PICategory>(){new PICategory()
                        {
                            Symbol = "PM1",
                            CategoryOfInvolvement = "Production",
                            RelatedCategorySymbol = "",
                            Description = "The company produces palm oil",
                            OwnershipYesNo = false,
                            RevenueRangeYesNo = true,
                            RevenueRange = new Dictionary<int, PIRange>()
                             {
                                 { 0, new PIRange(){ RangeId = 1, NA = false, Start = 0, Stop = 4.89}},
                                 { 1, new PIRange(){ RangeId = 2, NA = false, Start = 5, Stop = 9.89}},
                                 { 2, new PIRange(){ RangeId = 3, NA = false, Start = 10, Stop = 24.89}},
                                 { 3, new PIRange(){ RangeId = 4, NA = false, Start = 25, Stop = 49.89}},
                                 { 4, new PIRange(){RangeId = 5, NA = false, Start = 50, Stop = 100}}
                             }
                        },
                        new PICategory()
                        {
                            Symbol = "PM2",
                            CategoryOfInvolvement = "Significant ownership (production)",
                            RelatedCategorySymbol = "PM1",
                            Description = "The company owns 10-50 percent of a company that produces palm oil",
                            OwnershipYesNo = true,
                            RevenueRangeYesNo = false,
                            RevenueRange = new Dictionary<int, PIRange>()
                                {
                                    { 0, new PIRange(){ RangeId = 1, NA = true, Start = -1, Stop = -1}},
                                    { 1, new PIRange(){ RangeId = 2, NA = true, Start = -1, Stop = -1}},
                                    { 2, new PIRange(){ RangeId = 3, NA = true, Start = -1, Stop = -1}},
                                    { 3, new PIRange(){ RangeId = 4, NA = true, Start = -1, Stop = -1}},
                                    { 4, new PIRange(){RangeId = 5, NA = true, Start = -1, Stop = -1}}
                                }
                        },
                        new PICategory()
                        {
                            Symbol = "PM3",
                            CategoryOfInvolvement = "Refining and retail",
                            RelatedCategorySymbol = "",
                            Description = "The company refines or trades palm oil",
                            OwnershipYesNo = false,
                            RevenueRangeYesNo = true,
                            RevenueRange = new Dictionary<int, PIRange>()
                                {
                                     { 0, new PIRange(){ RangeId = 1, NA = false, Start = 0, Stop = 4.89}},
                                     { 1, new PIRange(){ RangeId = 2, NA = false, Start = 5, Stop = 9.89}},
                                     { 2, new PIRange(){ RangeId = 3, NA = false, Start = 10, Stop = 24.89}},
                                     { 3, new PIRange(){ RangeId = 4, NA = false, Start = 25, Stop = 49.89}},
                                     { 4, new PIRange(){RangeId = 5, NA = false, Start = 50, Stop = 100}}
                                }
                        },
                        new PICategory()
                        {
                            Symbol = "PM4",
                            CategoryOfInvolvement = "Significant ownership (refining and retail)",
                            RelatedCategorySymbol = "PM3",
                            Description = "The company owns 10-50 percent of a company that refines or trades palm oil",
                            OwnershipYesNo = true,
                            RevenueRangeYesNo = false,
                            RevenueRange = new Dictionary<int, PIRange>()
                                {
                                    { 0, new PIRange(){ RangeId = 1, NA = true, Start = -1, Stop = -1}},
                                    { 1, new PIRange(){ RangeId = 2, NA = true, Start = -1, Stop = -1}},
                                    { 2, new PIRange(){ RangeId = 3, NA = true, Start = -1, Stop = -1}},
                                    { 3, new PIRange(){ RangeId = 4, NA = true, Start = -1, Stop = -1}},
                                    { 4, new PIRange(){RangeId = 5, NA = true, Start = -1, Stop = -1}}
                                }
                        }, new PICategory()
                        {
                            Symbol = "PL1",
                            CategoryOfInvolvement = "Operations",
                            RelatedCategorySymbol = "",
                            Description = "The company is involved in predatory lending activities",
                            OwnershipYesNo = false,
                            RevenueRangeYesNo = true,
                            RevenueRange = new Dictionary<int, PIRange>()
                                 {
                                     { 0, new PIRange(){ RangeId = 1, NA = false, Start = 0, Stop = 4.89}},
                                     { 1, new PIRange(){ RangeId = 2, NA = false, Start = 5, Stop = 9.89}},
                                     { 2, new PIRange(){ RangeId = 3, NA = false, Start = 10, Stop = 24.89}},
                                     { 3, new PIRange(){ RangeId = 4, NA = false, Start = 25, Stop = 49.89}},
                                     { 4, new PIRange(){RangeId = 5, NA = false, Start = 50, Stop = 100}}
                                 }
                        },
                        new PICategory()
                        {
                            Symbol = "PL1",
                            CategoryOfInvolvement = "Significant ownership (Operations)",
                            RelatedCategorySymbol = "PL1",
                            Description = "The company owns 10-50 percent of another company with involvement in predatory lending activities.",
                            OwnershipYesNo = true,
                            RevenueRangeYesNo = false,
                            RevenueRange = new Dictionary<int, PIRange>()
                                {
                                    { 0, new PIRange(){ RangeId = 1, NA = true, Start = -1, Stop = -1}},
                                    { 1, new PIRange(){ RangeId = 2, NA = true, Start = -1, Stop = -1}},
                                    { 2, new PIRange(){ RangeId = 3, NA = true, Start = -1, Stop = -1}},
                                    { 3, new PIRange(){ RangeId = 4, NA = true, Start = -1, Stop = -1}},
                                    { 4, new PIRange(){RangeId = 5, NA = true, Start = -1, Stop = -1}}
                                }
                        }};
        }

        [TestCleanup]
        public void Cleanup()
        {
        }

        [TestMethod]
        [Owner("Mihai Stanescu")]
        public void GetIndicatorCategories_PIIndicatorCategoriesControllerTests()
        {
            var productInvolvement = new Sustainalytics.ProductInvolvement.BL.Fakes.StubIProductInvolvementBussinesLogic()
            {
                GetIndicatorCategoriesString = (symbol) =>
                    {
                        return piCategories.Where(pi => pi.Symbol.Contains(symbol)).ToList();
                    }
            };

            var piIndicatorCategoriesController = TestUtils.setupController<PIIndicatorCategoriesController>(new PIIndicatorCategoriesController(productInvolvement));

            HttpResponseMessage categories = piIndicatorCategoriesController.Get("PM", true);
            List<Sustainalytics.ProductInvolvement.Service.Models.PIIndicatorCategoryDto> categoriesList = new List<Sustainalytics.ProductInvolvement.Service.Models.PIIndicatorCategoryDto>();
            Assert.IsNotNull(categories, "The categories list should not be null.");
            Assert.IsTrue(categories.IsSuccessStatusCode, "The request has failed.");
            Assert.IsTrue(categories.TryGetContentValue<List<Sustainalytics.ProductInvolvement.Service.Models.PIIndicatorCategoryDto>>(out categoriesList), "The response does not contain the list of categories dto.");
        }
    }
}
